const { resolve } = require('path')
import { defineConfig } from 'vite'
import styleImport from 'vite-plugin-style-import'
import vue from '@vitejs/plugin-vue'
import legacy from '@vitejs/plugin-legacy'
import compressPlugin from 'vite-plugin-compression'

/*
 * vite 配置文档
 * https://vitejs.dev/config/
 * */
export default defineConfig({
  root: './',
  base: './',
  resolve: {
    alias: [{ find: '@', replacement: resolve(__dirname, 'src') }],
  },
  server: {
    host: '0.0.0.0',
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  plugins: [
    vue(),
    /*
     * 按需加载
     * https://github.com/anncwb/vite-plugin-style-import/blob/main/README.zh_CN.md
     */
    styleImport({
      libs: [
        {
          libraryName: 'vant',
          esModule: true,
          resolveStyle: (name) => {
            return `vant/es/${name}/style`
          },
        },
      ],
    }),
    // 解决较老手机浏览器白屏的问题
    // https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
    legacy({
      targets: ['ie >= 11'],
      additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
    }),
    compressPlugin({
      ext: '.gz',
      deleteOriginFile: false, // 是否删除原始文件
    }),
  ],
  build: {
    assetsInlineLimit: 4096,
    brotliSize: true,
    chunkSizeWarningLimit: 50,
  },
})
